import { BillApi } from '@/api/Bill.api.js'
import { Common } from '@/utils/common.js'
import config from '@/config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro.replace('/api', '/')

export default {
  name: '',

  data () {
    return {
      isShowTableShebaomingxiModal: false,
      companyName: null,
      companyId: null,
      year: null,
      month: null,
      billDate: null,
      tableData: [],
      tableComone: {
        pageIndex: 1, // 页码
        totalRows: 0, // 总条数
        pageSize: 10 // 当前页面展示条数
      },
      formItem: [
        { text: '单位合计', key: 'companyTotalAmt', value: '' },
        { text: '个人合计', key: 'employeeTotalAmt', value: '' },
        { text: '总计', key: 'totalAmt', value: '' }
      ],
      handelBills: [],
      ignoreInsureNo: 'false',
      arrListDown: {
        ignoreInsureNoOps: [
          {label: '显示', value: 'false'},
          {label: '隐藏', value: 'true'}
        ]
      }
    }
  },

  watch: {
  },

  mounted () {
  },

  methods: {
    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        CustomerIds: this.companyId,
        // Year: this.year,
        Month: this.billDate,
        IgnoreInsureNo: this.ignoreInsureNo,
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await BillApi.getMergedSIDetails(params)
      console.log('社保明细列表res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data.items
        this.tableComone.totalRows = res.data.totalRows
        this.companyName = res.data.data.companyName
        this.tableData.map(row => {
          row.insureItems.map(item => { // 循环解析insureItems里面的companyFee、employeeFee值并赋到相应的key
            var sumStr = ''
            if (item.companyFee) {
              Object.keys(item.companyFee).forEach(key => {
                sumStr = item.category + 'companyFee' + key
                row[sumStr] = item.companyFee[key]
              })
            }

            if (item.employeeFee) {
              Object.keys(item.employeeFee).forEach(key => {
                sumStr = item.category + 'employeeFee' + key
                row[sumStr] = item.employeeFee[key]
              })
            }
          })
        })

        this.formItem.map(col => {
          col.value = res.data.data[col.key]
        })
        
        if (res.data.data.handelBills) {
          var handelBills = res.data.data.handelBills
          handelBills.map(item => {
            item.year = this.year
            item.month = this.month
            item.billFees.map(c_item => {
              if (c_item.category.indexOf('TotalAmt') !== -1) {
                var str = c_item.category
                var str2 = str.replace(str[0],str[0].toLowerCase())
                item[str2] = c_item.amt
              } else {
                var str = c_item.isPersonalBurden ? c_item.category + 'employeeFeeamt' : c_item.category + 'companyFeeamt'
                item[str] = c_item.amt
              }
            })
            console.log('item: ', item)
            item.employeeName = item.type
            this.tableData.push(item)
          })
          console.log('handelBills: ', handelBills)
        }
      } else {
        this.tableData = []
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
      this.loading = false
    },

    ok () {
    },

    cancel () {
    },

    handleExportData () {
    },

    parsingItems (row, item, oneTitle, twoTitle1, twoTitle2, type) {
      var sumTitle = oneTitle + twoTitle1 + type
      console.log('sumTitle: ', sumTitle)
      row[sumTitle] = item[twoTitle1][type]
    },

    // 导出
    async exportMergedSIDetails () {
      var params = {
        CustomerIds: this.companyId,
        companyName: this.companyName,
        // year: this.year,
        // month: this.month
        Month: this.billDate,
        IgnoreInsureNo: this.ignoreInsureNo
        // pageIndex: this.tableComone.pageIndex,
        // pageSize: this.tableComone.pageSize,
      }
      console.log('导出params: ', params)
      let res = await BillApi.exportMergedSIDetails(params)
      if (res.status === 200 && res.data.success) {
        console.log('❤ 返回链接: ', baseUrl + res.data.message)
        window.open(baseUrl + res.data.message, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    }
  }
}
